package com.tanhua.server.service;

import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class MovementMQService {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendSavePublishMsg(String publishId){
        sendMessage(publishId, 1);
    }

    public void sendBrowsePublishMsg(String publishId){
        sendMessage(publishId, 2);
    }

    public void sendLikePublishMsg(String publishId){
        sendMessage(publishId, 3);
    }

    public void sendLovePublishMsg(String publishId) {
        sendMessage(publishId, 4);
    }

    public void sendCommentPublishMsg(String publishId) {
        sendMessage(publishId, 5);
    }

    public void sendUnlikePublishMsg(String publishId) {
        sendMessage(publishId, 6);
    }

    public void sendUnlovePublishMsg(String publishId){
        sendMessage(publishId, 7);
    }

    public void sendMessage(String publishId, Integer type) {
        Publish publish = publishApi.findById(publishId);
        Map<String, Object> map = new HashMap();
        map.put("publishiId", publish.getPid());
        map.put("type", type);
        map.put("userId", UserHolder.getUser().getId());
        rocketMQTemplate.convertAndSend("tanhua-quanzi", map);
        log.info("[MQ消息]发送动态,topic:{},map:{}", "tanhua-quanzi", map);
    }
}
